package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.tms.sc.SearchCustomerCriteria;
import cn.buk.qms.service.CustomerService;
import cn.buk.tms.dto.CustomerTpCarDto;
import cn.buk.tms.dto.CustomerTpFlightDto;
import cn.buk.tms.dto.CustomerTpHotelDto;
import cn.buk.tms.dto.CustomerTpTrainDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CustomerTravelPolicyController extends BaseController {

  @Autowired
  public CustomerTravelPolicyController(
                            CustomerService customerService) {
    this.customerService = customerService;
  }

  private final CustomerService customerService;

  /**
   * 保存客户的酒店差旅政策
   * @param request
   * @param customerId
   * @param positionId
   * @param zoneId
   * @param roomType
   * @param maxReimbursement
   * @param remark
   * @return
   */
  @PostMapping("/customer/{customerId}/tp/hotel")
  public JsonResult saveCustomerTpHotel(HttpServletRequest request,
                                        @PathVariable("customerId") int customerId,
                                        @RequestParam("positionId") int positionId,
                                        @RequestParam("zoneId") int zoneId,
                                        @RequestParam("roomType") int roomType,
                                        @RequestParam("maxReimbursement") int maxReimbursement,
                                        @RequestParam(value = "remark", required = false) String remark
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    CustomerTpHotelDto dto = new CustomerTpHotelDto();
    dto.setCustomerId(customerId);
    dto.setPositionId(positionId);
    dto.setZoneId(zoneId);
    dto.setRoomType(roomType);
    dto.setMaxReimbursement(maxReimbursement);
    dto.setRemark(remark);

    return customerService.saveCustomerTpHotel(enterpriseId, username, dto);
  }

  /**
   * 删除客户的酒店差旅政策
   * @param request
   * @param customerId
   * @param id
   * @return
   */
  @DeleteMapping(value = "/customer/{customerId}/tp/hotel/{id}")
  public JsonResult deleteCustomerTpHotel(HttpServletRequest request,
                                          @PathVariable("customerId") int customerId,
                                          @PathVariable("id") int id
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return customerService.deleteCustomerTpHotel(enterpriseId, username, customerId, id);
  }

  /**
   * 查找客户的酒店差旅政策
   * @param request
   * @param customerId
   * @param sc
   * @return
   */
  @GetMapping("/customer/{customerId}/tp/hotels")
  public List<CustomerTpHotelDto> searchCustomerTpHotels(HttpServletRequest request,
                                                         @PathVariable("customerId") int customerId,
                                                         @ModelAttribute("sc") SearchCustomerCriteria sc
  ) {
    final int enterpriseId = getEnterpriseId(request);
    sc.setCustomerId(customerId);

    return customerService.searchCustomerTpHotels(enterpriseId, sc);
  }


  /**
   * 保存客户的机票差旅政策
   * @param request
   * @param customerId 客户id
   * @param positionId 员工职位级别 id
   * @param flyTime 飞行时长
   * @param cabinClassBefore 规定飞行时长以内，或不限制飞行时长时
   * @param cabinClassAfter 超过飞行时长时
   * @param needLowest 是否要求当时最低
   * @param remark 备注
   * @return
   */
  @PostMapping("/customer/{customerId}/tp/flight")
  public JsonResult saveCustomerTpFlight(HttpServletRequest request,
                                        @PathVariable("customerId") int customerId,
                                        @RequestParam("positionId") int positionId,
                                        @RequestParam("flyTime") int flyTime,
                                        @RequestParam("cabinClassBefore") String cabinClassBefore,
                                        @RequestParam("cabinClassAfter") String cabinClassAfter,
                                        @RequestParam("needLowest") int needLowest,
                                        @RequestParam(value = "remark", required = false) String remark
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    CustomerTpFlightDto dto = new CustomerTpFlightDto();
    dto.setCustomerId(customerId);
    dto.setPositionId(positionId);
    dto.setFlyTime(flyTime);
    dto.setCabinClassBefore(cabinClassBefore);
    dto.setCabinClassAfter(cabinClassAfter);
    dto.setNeedLowest(needLowest);
    dto.setRemark(remark);

    return customerService.saveCustomerTpFlight(enterpriseId, username, dto);
  }

  /**
   * 查找客户的机票差旅政策
   * @param request
   * @param customerId
   * @param sc
   * @return
   */
  @GetMapping("/customer/{customerId}/tp/flights")
  public List<CustomerTpFlightDto> searchCustomerTpFlights(HttpServletRequest request,
                                                         @PathVariable("customerId") int customerId,
                                                         @ModelAttribute("sc") SearchCustomerCriteria sc
  ) {
    final int enterpriseId = getEnterpriseId(request);
    sc.setCustomerId(customerId);

    return customerService.searchCustomerTpFlights(enterpriseId, sc);
  }

  /**
   * 删除客户的机票差旅政策
   * @param request
   * @param customerId
   * @param id
   * @return
   */
  @DeleteMapping(value = "/customer/{customerId}/tp/flight/{id}")
  public JsonResult deleteCustomerTpFlight(HttpServletRequest request,
                                          @PathVariable("customerId") int customerId,
                                          @PathVariable("id") int id
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return customerService.deleteCustomerTpFlight(enterpriseId, username, customerId, id);
  }

  /**
   * 保存客户的用车差旅政策
   * @param request
   * @param customerId
   * @param positionId
   * @param vehicleGroupId 车型组id
   * @param remark
   * @return
   */
  @PostMapping("/customer/{customerId}/tp/car")
  public JsonResult saveCustomerTpCar(HttpServletRequest request,
                                        @PathVariable("customerId") int customerId,
                                        @RequestParam("positionId") int positionId,
                                        @RequestParam("vehicleGroupId") int vehicleGroupId,
                                        @RequestParam(value = "remark", required = false) String remark
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    CustomerTpCarDto dto = new CustomerTpCarDto();
    dto.setCustomerId(customerId);
    dto.setPositionId(positionId);
    dto.setVehicleGroupId(vehicleGroupId);
    dto.setRemark(remark);

    return customerService.saveCustomerTpCar(enterpriseId, username, dto);
  }

  /**
   * 删除客户的用车差旅政策
   * @param request
   * @param customerId
   * @param id
   * @return
   */
  @DeleteMapping(value = "/customer/{customerId}/tp/car/{id}")
  public JsonResult deleteCustomerTpCar(HttpServletRequest request,
                                           @PathVariable("customerId") int customerId,
                                           @PathVariable("id") int id
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return customerService.deleteCustomerTpCar(enterpriseId, username, customerId, id);
  }

  /**
   * 查找客户的用车差旅政策
   * @param request
   * @param customerId
   * @param sc
   * @return
   */
  @GetMapping("/customer/{customerId}/tp/cars")
  public List<CustomerTpCarDto> searchCustomerTpCars(HttpServletRequest request,
                                                         @PathVariable("customerId") int customerId,
                                                         @ModelAttribute("sc") SearchCustomerCriteria sc
  ) {
    final int enterpriseId = getEnterpriseId(request);
    sc.setCustomerId(customerId);

    return customerService.searchCustomerTpCars(enterpriseId, sc);
  }

  /**
   * 保存客户的火车票差旅政策
   * @param request
   * @param customerId
   * @param positionId
   * @param remark
   * @return
   */
  @PostMapping("/customer/{customerId}/tp/train")
  public JsonResult saveCustomerTpTrain(HttpServletRequest request,
                                      @PathVariable("customerId") int customerId,
                                      @RequestParam("positionId") int positionId,
                                      @RequestParam("trainType") int trainType,
                                      @RequestParam("seatLevel") int seatLevel,
                                      @RequestParam(value = "remark", required = false) String remark
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    CustomerTpTrainDto dto = new CustomerTpTrainDto();
    dto.setCustomerId(customerId);
    dto.setPositionId(positionId);
    dto.setTrainType(trainType);
    dto.setSeatLevel(seatLevel);
    dto.setRemark(remark);

    return customerService.saveCustomerTpTrain(enterpriseId, username, dto);
  }

  /**
   * 删除客户的火车票差旅政策
   * @param request
   * @param customerId
   * @param id
   * @return
   */
  @DeleteMapping(value = "/customer/{customerId}/tp/train/{id}")
  public JsonResult deleteCustomerTpTrain(HttpServletRequest request,
                                        @PathVariable("customerId") int customerId,
                                        @PathVariable("id") int id
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return customerService.deleteCustomerTpTrain(enterpriseId, username, customerId, id);
  }

  /**
   * 查找客户的火车票差旅政策
   * @param request
   * @param customerId
   * @param sc
   * @return
   */
  @GetMapping("/customer/{customerId}/tp/trains")
  public List<CustomerTpTrainDto> searchCustomerTpTrains(HttpServletRequest request,
                                                     @PathVariable("customerId") int customerId,
                                                     @ModelAttribute("sc") SearchCustomerCriteria sc
  ) {
    final int enterpriseId = getEnterpriseId(request);
    sc.setCustomerId(customerId);

    return customerService.searchCustomerTpTrains(enterpriseId, sc);
  }
}
